/*
 * FileName：WebsiteBannerServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import javax.annotation.Resource;

import com.smart.common.util.AjaxResult;
import com.smart.common.util.StringUtil;
import com.smart.common.util.Util;
import com.smart.wx.wxcms.dao.WebsiteBannerMapper;
import com.smart.wx.wxcms.entity.WebsiteBanner;
import com.smart.wx.wxcms.service.WebsiteBannerService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 网站模板-----banner表
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-12 23:13:45
 */
@Service
public class WebsiteBannerServiceImpl implements WebsiteBannerService {

    @Resource
    private WebsiteBannerMapper websiteBannerDao;

    @Override
    public WebsiteBanner queryObject(WebsiteBanner websiteBanner) throws Exception {
        return websiteBannerDao.queryObject(websiteBanner);
    }

    @Override
    public List<WebsiteBanner> queryList(WebsiteBanner websiteBanner) throws Exception {
        return websiteBannerDao.queryList(websiteBanner);
    }

    @Override
    public List<WebsiteBanner> queryListByPage(WebsiteBanner websiteBanner) throws Exception {
        return websiteBannerDao.queryListByPage(websiteBanner);
    }

    @Override
    public Integer queryCount(WebsiteBanner websiteBanner) throws Exception {
        Integer ct = websiteBannerDao.queryCount(websiteBanner);
        return ct == null ? 0 : ct;
    }

    @Override
    public Integer insert(WebsiteBanner websiteBanner) throws Exception {
        return websiteBannerDao.insert(websiteBanner);
    }

    @Override
    public Integer update(WebsiteBanner websiteBanner) throws Exception {
        return websiteBannerDao.update(websiteBanner);
    }

    @Override
    public Integer delete(WebsiteBanner websiteBanner) throws Exception {

        String bannerId = websiteBanner.getId();

        if (StringUtil.isNotBlank(bannerId)) {
            this.websiteBannerDao.deleteBatch(Arrays.asList(bannerId.split(",")));
        }
        return 1;
    }

    @Override
    public Integer deleteBatch(List<String> ids) throws Exception {
        return websiteBannerDao.deleteBatch(ids);
    }

    @Override
    public AjaxResult saveOrUpdate(WebsiteBanner banner) throws Exception {

        String bannerId = banner.getId();
        if (StringUtil.isNotBlank(bannerId)) { // 修改
            banner.setUpdateTime(new Date());
            this.websiteBannerDao.update(banner);
        } else { // 新增
            banner.setId(Util.getUUID());
            banner.setCreateTime(new Date());
            banner.setUpdateTime(new Date());
            this.websiteBannerDao.insert(banner);
        }
        return AjaxResult.saveSuccess();
    }
}
